JR.container.add = function($){
	return {
		init : function(){
			JR.container.add.type.init();
			JR.container.add.form.init();
			JR.container.add.back.init();
		}
	};
}(jQuery);

JR.container.add.type = function($){
	
	var model;
	
	var view = {
		init : function(){
			this.container = $('#addContainer #type');
			this.preload();
		},
		preload : function(){
			this.addOption('正在读取集装箱类别...', 0);
			this.container.uniform();
		},
		render : function(){
			this.container.children('option').remove();
			for(var index=0; index<model.length; index++){
				this.addOption(model[index].name, model[index].id);
			}
			this.container.val(JR.statics.id).change();
		},
		addOption : function(text,value){
			$('<option>').text(text).val(value).appendTo(this.container);
		}
	};
	
	var controller = {
		init : function(){
			this.loadData();
		},
		loadData : function(){
			JR.ajax(JR.url2('menu.action'), this.analyze);
		},
		analyze : function(data){
			model = data.types;
			view.render();
		}
	};
	
	return {
		init : function(){
			view.init();
			controller.init();
		}
	};	
}(jQuery);

JR.container.add.form = function($){
	
	var form = {
		init : function(){
			this.container = $('#addContainer');
			this.form = $('#addContainer form:first');
			this.submit = this.form.find('#btn-submit');
			this.back = this.form.find('#btn-back');
			this.url = JR.url2('container-add-exec.action');
			this.url_containers = JR.url2('container-list.action');
			this.initForm();
			this.addEvents();
			this.addValidation();
		},
		initForm : function(){
			var th = this;
			var options = {
				url : th.url,
				type : 'post',
				beforeSubmit : function(){
					return th.beforeSubmit()
				},
				success : function(){
					return th.success();
				}
			};
			
			this.form.submit(function(){
				$(this).ajaxSubmit(options);
				return false;
			});
		},
		addEvents : function(){
			var th = this;
			this.submit.click(function(){
				th.form.submit();
			});
			this.back.click(function(){
				JR.container.add.back.exec();
			});
			$('#addContainer input:file').change(function(){
				return th.validator.element('input[name="upload"]');
			});
		},
		beforeSubmit : function(){
			if(!this.form.valid()){
				return false;
			}else{
				this.container.block({
					message:null
				});
				return true;
			}
		},
		success : function(){
			this.container.unblock();
			this.container.block({
				message: '添加集装箱成功!<br/>页面正在跳转中...'
			});
			setTimeout(function(){
				JR.admin.loadx.showList($('#addContainer #type').val());
			}, 1000);
		},
		addValidation : function(){
			
			$.validator.addMethod('containerType', function(value, element){
				return this.optional(element) || parseInt(value)!=0; 
			},'请选择集装箱类别');
			
			$.validator.addMethod('imageFile', function(value, element){
				var suffix = value.substring(value.lastIndexOf('.')+1, value.length).toLowerCase();
				return this.optional(element) || (suffix == 'jpg' || suffix == 'png' || suffix == 'gif' || suffix == 'bmp'); 
			},'请选择图片文件(jpg、png、bmp、gif)');
			
			this.validator = this.form.validate({
				rules:{
					type: {
						containerType : true
					},
					name: {
						required: true
					},
					upload:{
						required: true,
						imageFile: true
					}
				},
				messages:{
					type: {
						containerType : '请选择集装箱类别'
					},
					name: {
						required: '请填写集装箱名称'
					},
					upload:{
						required: '请选择图片文件',
						imageFile: '请选择正确格式的图片文件(jpg、png、bmp、gif)'
					}
				},
				errorPlacement:function(error, element){
					var line = element.parents('div.line');
					var errorLine = $('<div>').addClass('line').css('padding','0 0 0 110px');
					error.appendTo(errorLine);
					line.after(errorLine);
				}
			});
		}
	};
	
	return {
		init : function(){
			form.init();
		}
	};
}(jQuery);

JR.container.add.back = function($){
	
	return {
		init : function(){
			$('.back').click(function(e){
				e.preventDefault();
				JR.admin.loadx.showList(JR.statics.id);
			});
		},
		exec : function(){
			JR.admin.loadx.showList(JR.statics.id);
		}
	};
}(jQuery);